package cn.danger.com.fragment.schedule;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.MainActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BaseFragment;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.presenter.SchedulePresenter;
import cn.danger.com.presenter.SchedulePresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.util.DateUtil;
import cn.danger.com.view.CommonView;
import cn.danger.com.view.adapter.ScheduleAdapter;

/**
 * Created by 俊 on 2016/12/26.
 */
public class ScheduleFragment extends BaseFragment implements CommonView {

    private View view;
    private MainActivity activity;
    private SwipeRefreshLayout swipe_layout;
    private ListView schedule_listView;
    private ProgressDialog dialog;
    private List<Schedule> schedules;
    private SchedulePresenter presenter;
    private ScheduleAdapter adapter;
    private MyReceiver receiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_schedule, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (MainActivity) getActivity();
        swipe_layout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);
        schedule_listView = (ListView) view.findViewById(R.id.schedule_listView);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new SchedulePresenterImpl(activity, this);

        schedules = presenter.getTodaySchedules();

        adapter = new ScheduleAdapter(activity, schedules, presenter);
        schedule_listView.setAdapter(adapter);

        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("update_schedule_list");
        activity.registerReceiver(receiver, filter);
    }

    @Override
    public void bindEvent() {
        schedule_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Schedule schedule = schedules.get(position);
                long time = Long.parseLong(schedule.getDate_start()) - DateUtil.getRemindTime(schedule.getRemind_time());
                if (time >= System.currentTimeMillis()) {
                    Intent intent = new Intent(activity, CommonActivity.class);
                    intent.putExtra("target", "add_schedule");
                    intent.putExtra("schedule", schedule);
                    activity.startActivity(intent);
                }
            }
        });
        swipe_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                update();
                swipe_layout.setRefreshing(false);
                showMessage("刷新成功");
            }
        });
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_DELETE_SCHEDULE) || message.equals(Constants.SUCCESS_TO_DELETE_LOCAL)) {
            schedules = presenter.getTodaySchedules();
            adapter = new ScheduleAdapter(activity, schedules, presenter);
            schedule_listView.setAdapter(adapter);
            activity.handler.sendEmptyMessage(1);
        }
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            update();
        }
    }

    public void update() {
        schedule_listView.setAdapter(null);
        schedules = presenter.getTodaySchedules();
        Log.i("danger", "schedules--->>" + schedules);
        adapter = new ScheduleAdapter(activity, schedules, presenter);
        schedule_listView.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (receiver != null) {
            activity.unregisterReceiver(receiver);
        }
    }
}
